package com.sc.nft.dao;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.RankingBoardConf;
import com.sc.nft.entity.dto.RankingListScreenDTO;
import com.sc.nft.entity.vo.*;
import com.sc.nft.enums.RankingBoardTypeEnum;
import com.sc.nft.mapper.RankingBoardConfMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 排行榜配置表(sc_ranking_board_conf)数据DAO
 *
 * @author Sun
 * @since 2023-08-03 16:44:17
 * @description 由 Mybatisplus Code Generator 创建
 */
@Slf4j
@Repository
public class RankingBoardConfDao extends ServiceImpl<RankingBoardConfMapper, RankingBoardConf> {

    public RankingBoardConf getLastOneByCollectionIdAndIsEnable(Long linkCollectionId,RankingBoardTypeEnum rankingBoardTypeEnum) {
        LambdaQueryWrapper<RankingBoardConf> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RankingBoardConf::getLinkCollectionId, linkCollectionId);
        wrapper.eq(RankingBoardConf::getType, rankingBoardTypeEnum);
        wrapper.orderByDesc(RankingBoardConf::getId);
        wrapper.last("limit 1");
        return getOne(wrapper);
    }

    public RankingBoardConf getLastOneByCollectionIdAndIsEnableAndType(RankingBoardTypeEnum rankingBoardTypeEnum) {
        LambdaQueryWrapper<RankingBoardConf> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RankingBoardConf::getType, rankingBoardTypeEnum);
        wrapper.orderByDesc(RankingBoardConf::getId);
        wrapper.last("limit 1");
        return getOne(wrapper);
    }

    public Page<PageRankingBoardListByIncomeTypeVO> getPageRankingBoardListByIncomeType(RankingBoardTypeEnum rankingBoardTypeEnum, Integer pageNo, Integer pageSize) {
        return baseMapper.getPageRankingBoardListByIncomeType(new Page<>(pageNo, pageSize), rankingBoardTypeEnum.getCode());
    }

    public Page<PageRankingBoardListByTypeVO> getPageRankingBoardListByType(RankingBoardTypeEnum type, Integer pageNo, Integer pageSize,String name,String beginTime,String endTime,String linkCollectonName,Integer isEnable) {
        return baseMapper.getPageRankingBoardListByType(new Page<>(pageNo, pageSize), ObjectUtil.isNull(type) ? 0 : type.getCode(),name,beginTime,endTime,linkCollectonName,isEnable);
    }

    public boolean updateIsEnableById(Long id, Boolean isEnable) {
        LambdaUpdateWrapper<RankingBoardConf> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(RankingBoardConf::getId, id);
        wrapper.set(RankingBoardConf::getIsEnable, isEnable);
        wrapper.set(RankingBoardConf::getModifyTime, DateTime.now());
        return update(wrapper);
    }

    public boolean updateIsOpenRewardsById(Long id, Boolean isOpenRewards) {
        LambdaUpdateWrapper<RankingBoardConf> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(RankingBoardConf::getId, id);
        wrapper.set(RankingBoardConf::getIsOpenRewards, isOpenRewards);
        wrapper.set(RankingBoardConf::getModifyTime, DateTime.now());
        return update(wrapper);
    }

    public boolean updateSortById(Long id, Integer sort) {
        LambdaUpdateWrapper<RankingBoardConf> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(RankingBoardConf::getId, id);
        wrapper.set(RankingBoardConf::getSort, sort);
        wrapper.set(RankingBoardConf::getModifyTime, DateTime.now());
        return update(wrapper);
    }

    public boolean updateRankingBoardMinimumQuantity(Long id, Integer minimumQuantity, String ruleSpecification) {
        LambdaUpdateWrapper<RankingBoardConf> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(RankingBoardConf::getId, id);
        wrapper.set(RankingBoardConf::getMinimumQuantity, minimumQuantity);
        wrapper.set(RankingBoardConf::getModifyTime, DateTime.now());
        wrapper.set(RankingBoardConf::getRuleSpecification, ruleSpecification);
        return update(wrapper);
    }

    public boolean delRankingBoard(Long id) {
        int i = baseMapper.deleteById(id);
        if(i>0){
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    public List<RankingListScreenVO> rankingListScreen(RankingListScreenDTO rankingListScreenDTO) {
        return baseMapper.rankingListScreen(rankingListScreenDTO);
    }

    public List<RankingListScreenVO> historyRankingListScreen(RankingListScreenDTO rankingListScreenDTO) {

        return baseMapper.historyRankingListScreen(rankingListScreenDTO);
    }


    public List<RankingBoardConf> listIsOngoing() {
        LambdaQueryWrapper<RankingBoardConf> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(RankingBoardConf::getIsEnable, Boolean.TRUE);
        // 结束时间大于当前时间
        lambdaQueryWrapper.gt(RankingBoardConf::getEndTime, DateTime.now());
        // 开始时间小于等于当前时间
        lambdaQueryWrapper.le(RankingBoardConf::getBeginTime, DateTime.now());
        return list(lambdaQueryWrapper);
    }

    public List<RanKingVO> getRanKing(Long linkCollectionId, Integer minimumQuantity, Integer listNumber,Long rankingBoardConfId) {
        return baseMapper.getRanKing(linkCollectionId,minimumQuantity,listNumber,rankingBoardConfId);
    }

    public List<MallRanking> mallRanking(Date startTime, Date endTime, Integer listNumber, Integer minimumQuantity,Long rankingBoardConfId) {
        return baseMapper.mallRanking(DateUtil.beginOfDay(startTime), DateUtil.endOfDay(endTime),listNumber,minimumQuantity,rankingBoardConfId);
    }

    /**
     * 获取所有结束未结算的榜单
     *
     * @return
     */
    public List<RankingBoardConf> listIsExpiredAndNotSettlement() {
        LambdaQueryWrapper<RankingBoardConf> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(RankingBoardConf::getIsEnable, Boolean.TRUE);
        // 结束时间小于当前时间
        lambdaQueryWrapper.lt(RankingBoardConf::getEndTime, DateTime.now());
        // 未结算
        lambdaQueryWrapper.eq(RankingBoardConf::getIsSettlement, Boolean.FALSE);
        return list(lambdaQueryWrapper);
    }

    public BigDecimal getIpMallTradingProfit(Date beginTime, Date endTime) {
        return baseMapper.getIpMallTradingProfit(beginTime,endTime);
    }

    public Integer getTotalHoldCount(Long linkCollectionId, Date beginTime, Date endTime, Integer beginSection, Integer endSection) {
        return baseMapper.getTotalHoldCount(linkCollectionId,beginTime,endTime,beginSection,endSection);
    }

    public void updateIsSettlement(Long id) {
        LambdaUpdateWrapper<RankingBoardConf> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(RankingBoardConf::getId, id);
        wrapper.set(RankingBoardConf::getIsSettlement, Boolean.TRUE);
        update(wrapper);
    }

    public void updateModifyTime(Long id) {
        LambdaUpdateWrapper<RankingBoardConf> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(RankingBoardConf::getId, id);
        wrapper.set(RankingBoardConf::getModifyTime, DateTime.now());
        update(wrapper);
    }

    public BigDecimal individualTurnover(Long userId, Date beginTime, Date endTime) {
        return baseMapper.individualTurnover(userId,beginTime,endTime);
    }

    public BigDecimal turnover(Integer beginSection, Integer endSection, Date beginTime, Date endTime) {
        return baseMapper.turnover(beginSection,endSection,beginTime,endTime);
    }

    public void closeAnAccount(Long id) {
        LambdaUpdateWrapper<RankingBoardConf> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(RankingBoardConf::getId, id);
        wrapper.set(RankingBoardConf::getIsSettlement, Boolean.TRUE);
        wrapper.set(RankingBoardConf::getIsSettlementIncome, Boolean.TRUE);
        wrapper.set(RankingBoardConf::getIsSettlementRewards, Boolean.TRUE);
        update(wrapper);
    }

    public List<RanKingVO> getNewRanKing(Date beginTime, Date endTime, Long linkCollectionId, Integer minimumQuantity, Integer listNumber,Long rankingBoardConfId) {
        return baseMapper.getNewRanKing(beginTime,endTime,linkCollectionId,minimumQuantity,listNumber,rankingBoardConfId);
    }
    public Integer getBeginAndEndTotalHoldCount(Long linkCollectionId, Date beginTime, Date endTime, Integer beginSection, Integer endSection) {
        return baseMapper.getBeginAndEndTotalHoldCount(linkCollectionId,beginTime,endTime,beginSection,endSection);
    }
}